package cn.yy.cloudpay.view;

import cn.yy.cloudpay.R;
import cn.yy.cloudpay.listener.IKeyboardResultListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TableLayout;

public class KeyboardLayout implements OnClickListener {

	private TableLayout mLayout;
	//数字键盘监听接口
	private IKeyboardResultListener mListener;

	private final String NUM_ONE = "1";
	private final String NUM_TWO = "2";
	private final String NUM_THREE = "3";
	private final String NUM_FOUR = "4";
	private final String NUM_FIVE = "5";
	private final String NUM_SIX = "6";
	private final String NUM_SEVEN = "success";
	private final String NUM_EIGHT = "8";
	private final String NUM_NINE = "9";
	private final String NUM_TEN = "0";
	private final String NUM_POINT = ".";

	private ImageView iv_1;
	private ImageView iv_2;
	private ImageView iv_3;
	private ImageView iv_4;
	private ImageView iv_5;
	private ImageView iv_6;
	private ImageView iv_7;
	private ImageView iv_8;
	private ImageView iv_9;
	private ImageView iv_0;
	private ImageView iv_point;
	private ImageView iv_backspace;
	private ImageView iv_reset;
	private ImageView iv_confirm;
	private ImageView iv_refund;
	
	private boolean mIsReturn; // 是否有退款按钮

	public KeyboardLayout(TableLayout tableLayout, boolean isReturn) {
		this.mLayout = tableLayout;
		mIsReturn = isReturn;
		initView();
		initListener();
	}

	private void initView() {
		iv_1 = (ImageView) mLayout.findViewById(R.id.iv_1);
		iv_2 = (ImageView) mLayout.findViewById(R.id.iv_2);
		iv_3 = (ImageView) mLayout.findViewById(R.id.iv_3);
		iv_4 = (ImageView) mLayout.findViewById(R.id.iv_4);
		iv_5 = (ImageView) mLayout.findViewById(R.id.iv_5);
		iv_6 = (ImageView) mLayout.findViewById(R.id.iv_6);
		iv_7 = (ImageView) mLayout.findViewById(R.id.iv_7);
		iv_8 = (ImageView) mLayout.findViewById(R.id.iv_8);
		iv_9 = (ImageView) mLayout.findViewById(R.id.iv_9);
		iv_0 = (ImageView) mLayout.findViewById(R.id.iv_0);
		iv_1 = (ImageView) mLayout.findViewById(R.id.iv_1);
		iv_point = (ImageView) mLayout.findViewById(R.id.iv_point);
		iv_backspace = (ImageView) mLayout.findViewById(R.id.iv_backspace);
		iv_reset = (ImageView) mLayout.findViewById(R.id.iv_reset);
		iv_confirm = (ImageView) mLayout.findViewById(R.id.iv_confirm);
		iv_refund = (ImageView) mLayout.findViewById(R.id.iv_refund);
		if(!mIsReturn){
			iv_refund.setImageResource(R.drawable.keyboard_number_null);
			iv_refund.setClickable(false);
		}
	}

	private void initListener() {
		iv_1.setOnClickListener(this);
		iv_2.setOnClickListener(this);
		iv_3.setOnClickListener(this);
		iv_4.setOnClickListener(this);
		iv_5.setOnClickListener(this);
		iv_6.setOnClickListener(this);
		iv_7.setOnClickListener(this);
		iv_8.setOnClickListener(this);
		iv_9.setOnClickListener(this);
		iv_0.setOnClickListener(this);
		iv_point.setOnClickListener(this);
		iv_backspace.setOnClickListener(this);
		iv_confirm.setOnClickListener(this);
		iv_reset.setOnClickListener(this);
		if(mIsReturn){
			iv_refund.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_1:
			mListener.onNumberKeyResult(NUM_ONE);
			break;
		case R.id.iv_2:
			mListener.onNumberKeyResult(NUM_TWO);
			break;
		case R.id.iv_3:
			mListener.onNumberKeyResult(NUM_THREE);
			break;
		case R.id.iv_4:
			mListener.onNumberKeyResult(NUM_FOUR);
			break;
		case R.id.iv_5:
			mListener.onNumberKeyResult(NUM_FIVE);
			break;
		case R.id.iv_6:
			mListener.onNumberKeyResult(NUM_SIX);
			break;
		case R.id.iv_7:
			mListener.onNumberKeyResult(NUM_SEVEN);
			break;
		case R.id.iv_8:
			mListener.onNumberKeyResult(NUM_EIGHT);
			break;
		case R.id.iv_9:
			mListener.onNumberKeyResult(NUM_NINE);
			break;
		case R.id.iv_0:
			mListener.onNumberKeyResult(NUM_TEN);
			break;
		case R.id.iv_point:
			mListener.onNumberKeyResult(NUM_POINT);
			break;
		case R.id.iv_confirm:
			mListener.onConfirmResult();
			break;
		case R.id.iv_backspace:
			mListener.onBackSpaceResult();
			break;
		case R.id.iv_reset:
			mListener.onResetKeyResult();
			break;
		case R.id.iv_refund:
			mListener.onRefund();
			break;
		default:
			break;
		}
	}

	public void setKeyboardResultListener(IKeyboardResultListener listener) {
		this.mListener = listener;
	}
}
